package com.ddm.controller.admin;


import com.ddm.dto.AllLevelDTO;
import com.ddm.dto.LevelDTO;
import com.ddm.dto.page.LevelPageQueryDTO;
import com.ddm.exception.DataErrorException;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.LevelService;
import com.ddm.service.impl.IndicatorService;
import com.ddm.vo.LevelVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import static com.ddm.constant.LevelConstant.LEVEL_ERROR;
import static com.ddm.constant.MessageConstant.NULL_DATA;

/**
 * <p>
 * 四级指标表 前端控制器
 * </p>
 *
 * @author 丁丁猫
 * @since 2025-01-13
 */
@RestController("AdminLevelController")
@RequestMapping("/admin/level")
@Slf4j
public class LevelController {
    @Autowired
    private LevelService levelService;
    @Autowired
    private IndicatorService indicatorService;

    /**
     * 根据numId,level,standardId查询指标
     * @param numId
     * @param level
     * @param standardId
     * @return
     */
    @GetMapping("/select")
    @ApiOperation("根据numId,level,standardId查询指标")
    public Result<LevelVO> getBySelectId(@RequestParam Long level, @RequestParam String numId, @RequestParam Long standardId) {
        log.info("根据numId,level,standardId查询指标:{},{},{}", numId, level, standardId);
        if (level < 1 || level > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        return Result.success(levelService.getBySelectId(level, numId, standardId));
    }

    /**
     * 插入指标
     *
     * @param levelDTO
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation("插入指标")
//    @Transactional
    public Result<String> insert(@RequestBody LevelDTO levelDTO) {
        if (levelDTO.getStandardId() == null || levelDTO.getLevel() == null || levelDTO.getNumId() == null || levelDTO.getName() == null) {
            throw new DataErrorException(NULL_DATA);
        }
        if (levelDTO.getLevel() < 1 || levelDTO.getLevel() > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        log.info("新增指标：{}", levelDTO.getName());
        levelService.saveI(levelDTO);
        return Result.success();
    }

    /**
     * 新增指标
     *
     * @param levelDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增指标")
//    @Transactional
    public Result<LevelDTO> add(@RequestBody LevelDTO levelDTO) {
        if (levelDTO.getStandardId() == null || levelDTO.getLevel() == null || levelDTO.getNumId() == null || levelDTO.getName() == null) {
            throw new DataErrorException(NULL_DATA);
        }
        if (levelDTO.getLevel() < 1 || levelDTO.getLevel() > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        log.info("新增指标：{}", levelDTO.getName());
        Long id = levelService.saveV(levelDTO);
        levelDTO.setId(id);
        return Result.success(levelDTO);
    }

    /**
     * 根据id修改指标
     *
     * @param levelDTO
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改指标")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<String> updateById(@RequestBody LevelDTO levelDTO) {
        if (levelDTO.getStandardId() == null || levelDTO.getLevel() == null || levelDTO.getNumId() == null || levelDTO.getName() == null || levelDTO.getId() == null) {
            throw new DataErrorException(NULL_DATA);
        }
        if (levelDTO.getLevel() < 1 || levelDTO.getLevel() > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        log.info("根据id修改指标:{}", levelDTO.getId());
        levelService.updateById(levelDTO);
        return Result.success();
    }

    /**
     * 根据关键词分页查询指标
     *
     * @param levelPageQueryDTO
     * @return
     */
    @GetMapping
    @ApiOperation("根据关键词分页查询指标")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> pageList(LevelPageQueryDTO levelPageQueryDTO) {
        if (levelPageQueryDTO.getLevel() < 1 || levelPageQueryDTO.getLevel() > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        log.info("根据关键词分页查询指标");
        return Result.success(levelService.pageQueryByKeyWord(levelPageQueryDTO));
    }

    /**
     * 根据id查询指标
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询指标")
    public Result<LevelVO> getById(@PathVariable Long id, @RequestParam Long level) {
        log.info("根据id查询指标:{},level:{}", id, level);
        if (level < 1 || level > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        return Result.success(levelService.getById(id, level));
    }

    /**
     * 根据id删除指标
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除术语")
    @Transactional
    public Result<String> deleteById(@PathVariable Long id, @RequestParam Integer level, @RequestParam String numId) {
        log.info("根据id删除指标:{},level:{}", id, level);
        if (level < 1 || level > 4) {
            throw new DataErrorException(LEVEL_ERROR);
        }
        indicatorService.deleteIndicator(numId, level, id);
        return Result.success();
    }

    /**
     * 保存所有指标
     *
     * @param allLevelDTO
     * @return
     */
    @Transactional
    @PostMapping("saveAll")
    @ApiOperation("保存所有指标")
    public Result<String> saveAll(@RequestBody AllLevelDTO allLevelDTO) {
        if (allLevelDTO.getStandardId() == null) {
            throw new DataErrorException(NULL_DATA);
        }
        log.info("保存所有指标");
        //删除所有指标
        levelService.removeByStandardId(allLevelDTO.getStandardId());
        //新增所有指标
        levelService.saveAll(allLevelDTO);
        return Result.success();
    }
}

